package com.litchi.adapter;

import java.util.List;

import com.litchi.R;
import com.litchi.bean.Address;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class AddressAdapter extends BaseAdapter {

	private List<Address> list;
	private Context mContext;

	public AddressAdapter(Context mContext, List<Address> list) {
		this.list = list;
		this.mContext = mContext;
	}
	
	public void setList(List<Address> list){
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHold viewHold;
		if (convertView == null) {
			viewHold=new ViewHold();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_address, null);
			viewHold.nameTextView=(TextView)convertView.findViewById(R.id.text_address_name);
			viewHold.phoneTextView=(TextView)convertView.findViewById(R.id.text_address_phone);
			viewHold.provinceTextView=(TextView)convertView.findViewById(R.id.text_address_province);
			viewHold.addressTextView=(TextView)convertView.findViewById(R.id.text_address);
		} else {
			viewHold = (ViewHold) convertView.getTag();
		}
		if (null!=list.get(position).getName()&&null!=list.get(position).getPhone()&&null!=list.get(position).getAddress()) {
			viewHold.nameTextView.setText(list.get(position).getName());
			viewHold.phoneTextView.setText(list.get(position).getPhone());
			viewHold.provinceTextView.setText(list.get(position).getProvince());
			viewHold.addressTextView.setText(list.get(position).getAddress());
		}
		
		return convertView;
	}

	class ViewHold {
		TextView nameTextView, phoneTextView, provinceTextView, addressTextView;

	}

}
